// pages/home/home.js
const app = getApp()
const getUserInfo = require('request/api/index').getUserInfo
const getTemplateList = require('request/api/index').getTemplateList
const getJgfbPage = require('request/api/quotation').getJgfbPage
const parseTime = require('utils/index').parseTime

Page({

  /**
   * 页面的初始数据
   */
  data: {
    sourceUrl: app.sourceUrl,
    fileHost: app.fileHost,
    pageNum: 1,
    pageSize: 10,
    total: 0,
    list: [],
    today: parseTime(new Date(), '{y}-{m}-{d}')
  },

  setMessage(tmplIds) {
    wx.requestSubscribeMessage({
    tmplIds: tmplIds.slice(0, 3),
      success: (res) => {
        console.log(res)
      },
      fail: (err) => {
        console.log(err)
      }
    })
  },

  getList() {
    getJgfbPage({
      pageNo: this.data.pageNum,
      pageSize: this.data.pageSize,
      // sj: this.data.today
    }).then(res => {
      res.data.list.forEach(item => {
        item.sj = parseTime(item.sj)
      })
      this.setData({
        list: this.data.list.concat(res.data.list),
        total: res.data.total,
        pageNum: this.data.pageNum + 1
      })
    })
  },

  loadList() {
    if (this.data.total <= this.data.pageNum * this.data.pageSize) {
      return
    }
    this.setData({
      pageNum: this.data.pageNum + 1
    })
    this.getList()
  },

  goToDetail (e) {
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.name
    wx.navigateTo({
      url: '/pages/quotation/detail/detail?id=' + id + '&name=' + name,
    })
  },

  getSetting () {
    let that = this
    getTemplateList().then(resList => {
      let subscribe = resList.data.map(item => item.value)

      wx.getSetting({
        withSubscriptions: true,
        success(res) {
          console.log('res.subscriptionsSetting')
          console.log(res.subscriptionsSetting.itemSettings)
          if (res.subscriptionsSetting.itemSettings) {
            let tmplIds = []
            subscribe.forEach(item => {
              if (!res.subscriptionsSetting.itemSettings[item]) {
                tmplIds.push(item)
              }
            })
            if (tmplIds.length > 0) {
              wx.showModal({
                title: '订阅消息',
                content: '订阅消息后，系统将提醒您查看商户每日收购价格',
                success(res) {
                  if (res.confirm) {
                    that.setMessage(tmplIds)
                  }
                }
              })
            }
          } else {
            wx.showModal({
              title: '订阅消息',
              content: '订阅消息后，系统将提醒您查看商户每日收购价格',
              success(res) {
                if (res.confirm) {
                  that.setMessage(subscribe)
                }
              }
            })
          }
        }
      })

    })
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    app.resetUserInfo = (callBack) => {
      getUserInfo().then(res => {
        app.globalData.userInfo = res.data
        app.globalData.loginState = true
        if (callBack) {
          callBack()
        }
      })
    }
    let that = this
    wx.getStorage({
      key: 'token',
      success(res) {
        let token = res.data
        if (token) {
          token = JSON.parse(res.data)
          for (let k in token) {
            app.globalData[k] = token[k]
          }
          app.resetUserInfo(() => {
            that.getList()
            that.getSetting()
          })
        } else {
          this.getList()
          this.getSetting()
        }
      }
    })

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function') {
      this.getTabBar((tabBar) => {
        tabBar.setData({
          selected: 0
        })
      })
    }
    if (app.globalData.loginState) {
      this.getList()
      this.getSetting()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.loadList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})